﻿using AutoMapper;
using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Write.Api.Application.Command.DocumentsCommand;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Write.Api.Application.CommandHander.DocumentsCommandHander
{
    public class CreateDocumentsCommandHander : IRequestHandler<CreateDocumentsCommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public CreateDocumentsCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<object>> Handle(CreateDocumentsCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var model = mapper.Map<Documents>(request);

            model.Del = false;
            model.Time = DateTime.Now;

            int documentsId = await db.Insertable(model).ExecuteReturnIdentityAsync();

            result.Data = new { documentsId };

            result.Code = APIEnum.success;
            result.Msg = "添加生产信息成功";

            return result;
        }
    }
}
